package foodserver;

import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.subway.babynote.syncdata.*;


import querysrc.query_addfeedback;

import querysrc.sqlConnectionHandler;
import querysrc.sqlConnectionHandlerManager;
import serverdata.httpreqhandler;



public class addFeedback extends addbase {

	private static final long serialVersionUID = 1L;	
	query_addfeedback qa = null;	
	
	
	public addFeedback() {
		// TODO Auto-generated constructor stub
	}
	protected void initialize_subquery()
	{
		qa = new query_addfeedback(this.sqlconhandler);
	}
	protected void destory_subquery()
	{
		this.release_query(qa);
	}
	@Override
	protected JSONObject read_data(HttpServletRequest request) {
		JSONObject jsono = new JSONObject();
		try
		{
			String uai_str = request.getParameter(PostParam.user_auth_info_Param.record);
			String feedback_str = request.getParameter(PostParam.feedbackRecord_Param.records);
			JSONObject uaijson = new JSONObject(uai_str);
			JSONArray feedbackjson = new JSONArray(feedback_str);
			jsono.put(PostParam.user_auth_info_Param.record, uaijson);
			jsono.put(PostParam.feedbackRecord_Param.records, feedbackjson);
		}
		catch(Exception ex)
		{
			
		}		
		// TODO Auto-generated method stub
		return jsono;
	}	
	protected int add_data(JSONObject input_jsono)
	{
		try
		 {		
			 int iret = 0;		
			 JSONObject jsono_uai = input_jsono.getJSONObject(PostParam.user_auth_info_Param.record);
			 
			 ClientDataRecord.user_auth_info uai = new ClientDataRecord.user_auth_info(jsono_uai);
			 
			 boolean bret = this.qau.sync_authen(uai.id, uai.imsi, uai.imei);
			 if(bret)
			 {
				 ArrayList<ClientDataRecord.feedbackRecord> al = new ArrayList<ClientDataRecord.feedbackRecord>();				 
				 JSONArray jsona = input_jsono.getJSONArray(PostParam.feedbackRecord_Param.records);
				 
				 int record_count = jsona.length();
				 for(int i = 0; i< record_count;i++)
				 {
					 JSONObject tempjsono = jsona.getJSONObject(i);
					 try
					 {
						 ClientDataRecord.feedbackRecord fbr = new ClientDataRecord.feedbackRecord(tempjsono);
						 al.add(fbr);					 
					 }
					 catch (JSONException e) {
							// TODO Auto-generated catch block
							System.out.println("JSON error for feedRecord:" + e.getMessage());
							e.printStackTrace();
						}				 
				 }
				 
				 iret = qa.addfeedback(al, uai.id);
			 }
			 else
				 iret = errMessage.err_authenfail.errcode;
			 return iret;
		 }
		 catch (JSONException e) {
				// TODO Auto-generated catch block
				System.out.println("JSON error for FeedSync doPost:" + e.getMessage());
				e.printStackTrace();
				return -1;
				
			}
		 catch (Exception e) {
			 System.out.println("FeedSync doPost error:" + e.getMessage());
		      e.printStackTrace();
		      return -1;
		    }
	}
		 
}
